package cn.arikeky.pet.controller;

import cn.arikeky.basic.util.LoginContext;
import cn.arikeky.org.domain.Employee;
import cn.arikeky.org.service.IEmployeeService;
import cn.arikeky.pet.domain.SearchMasterMsg;
import cn.arikeky.pet.dto.AcceptDto;
import cn.arikeky.pet.service.ISearchMasterMsgService;
import cn.arikeky.pet.query.SearchMasterMsgQuery;
import cn.arikeky.basic.util.AjaxResult;
import cn.arikeky.basic.util.PageList;
import cn.arikeky.user.domain.Logininfo;
import cn.arikeky.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeService employeeService;





    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * publish
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request)
    {
        //获取登录用户信息
        Logininfo loginUser = LoginContext.getLoginUser(request);

        searchMasterMsg.setUserId(loginUser.getId());

        //通过searchMasterMsg这个对象属性把user带进去了
//        searchMasterMsgService.publish(searchMasterMsg);
        return searchMasterMsgService.publish(searchMasterMsg);
    }


    /**
     * 查询审核通过后分配到店铺的数据
     * state = 1 and shopId = this.shopId
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {
        // 获取当前登录人信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee employee = employeeService.selectByLogininfo(loginUser.getId());
        searchMasterMsg.setShopId(employee.getShopId());
        searchMasterMsg.setState(1);
        return searchMasterMsgService.selectSearchMasterMassageByParam(searchMasterMsg);
    }

    /**
     * 查询待审核的数据
     * state = 0
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsg) {
        searchMasterMsg.setState(0);
        return searchMasterMsgService.selectSearchMasterMassageByParam(searchMasterMsg);
    }

    /**
     * 查询进入寻主池的数据
     * state = 1 and shopId = null
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsg) {
        searchMasterMsg.setState(1);
        return searchMasterMsgService.selectSearchMasterMassageByParam(searchMasterMsg);
    }

    /**
     * 查询已完成的数据
     * 查询条件：state=3
     * 		1.平台管理员：shopId=null
     * 		2.店铺管理员：shopId=当前登陆人shopId
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {
        // 获取当前登录人信息
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee employee = employeeService.selectByLogininfo(loginUser.getId());
        // 管理员shopId为null
        if (employee.getShopId() != null){
            searchMasterMsg.setShopId(employee.getShopId());
        }
        searchMasterMsg.setState(3);
        return searchMasterMsgService.selectSearchMasterMassageByParam(searchMasterMsg);
    }

    /**
     * 给用户个人中心提供的查询接口
     * TODO 自研用户展示 重新写查询SQL
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsg) {

        return searchMasterMsgService.selectSearchMasterMassageByParam(searchMasterMsg);
    }


    /**
     * 拒绝
     * @param msgId
     * @return
     */
    @GetMapping("reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId ,HttpServletRequest request){
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee emp = employeeService.findByLogId(loginUser.getId());
        searchMasterMsgService.rejectById(msgId,emp);
        return AjaxResult.me().success();
    }

    @PostMapping("/accept")
    private AjaxResult accept(@RequestBody AcceptDto acceptDto){
        searchMasterMsgService.accept(acceptDto);
        return AjaxResult.me().success();
    }

}
